<?php

namespace app\admin\controller\user;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="user_cashout")
 */
class Cashout extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\UserCashout();
        
    }

          /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k => $v){
                
                $user = Db::name("user")->where("id",$v["uid"])->find();
                if(!empty($user)){
                    $v['nickname'] = $user['nickname'];
                }else{
                    $v['nickname'] = "";
                }
                $list[$k] = $v;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
 
    /**
    * @NodeAnotation(title="通过")
    */
    public function adopt_url()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $ret = $this->model->where('id', $post['id'])->where('status', 0)->find();
            if (empty($ret)) {
                $this->error('当前状态不能操作');
            }
            Db::name("user_cashout")->where("id",$post["id"])->update(["status"=>1]);
            $this->success('操作成功', '');
        }
    }
 
      /**
    * @NodeAnotation(title="驳回")
    */
    public function reject_url()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post["reason_for_rejection"])){
                $this->error("驳回原因不能为空");
            }
            $ret = $this->model->where('id', $post['id'])->where('status', 0)->find();
            if (empty($ret)) {
                $this->error('当前状态不能操作');
            }
            Db::name("user_cashout")->where("id",$post["id"])->update(["status"=>2,"reason"=>$post["reason_for_rejection"]]);
            $this->success('驳回成功', '');
        }
        return $this->fetch();
    }

    
}